﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PortableVersion.Core
{

    public enum DiffEvent { Modified = 0, Deleted = 1, Renamed = 2, Added = 3, Unchanged = 4 }
    public enum DiffType { Safe, ContentConflict, Ignore, LocationConflict }
    /// <summary>
    /// Represents a diff between two versioninfo items
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Diff<T> where T : IVersioningItem
    {
        public T Source { get; private set; }
        public T Target { get; private set; }
        public T Meta { get; private set; }
        public bool IsFolder {get { return Source.IsFolder; }}
        public DiffEvent SourceEvent { get; private set; }
        public DiffEvent TargetEvent { get; private set; }
        public DiffType DiffType
        {
            get { return (SourceEvent == DiffEvent.Added)? DiffType.Safe : _loockup[GetLupKey(SourceEvent, TargetEvent)]; }
        }

        private static int GetLupKey(DiffEvent sourceEvent, DiffEvent targetEvent)
        {
            return (int) sourceEvent*100 + (int)targetEvent;
        }

        private readonly Dictionary<int, DiffType> _loockup = new Dictionary<int, DiffType>()
                                                         {
                                                             {GetLupKey(DiffEvent.Unchanged, DiffEvent.Unchanged), DiffType.Ignore},
                                                             {GetLupKey(DiffEvent.Unchanged, DiffEvent.Deleted), DiffType.Ignore},
                                                             {GetLupKey(DiffEvent.Unchanged, DiffEvent.Renamed), DiffType.Ignore},
                                                             {GetLupKey(DiffEvent.Unchanged, DiffEvent.Modified), DiffType.Ignore},

                                                             {GetLupKey(DiffEvent.Modified, DiffEvent.Unchanged), DiffType.Safe},
                                                             {GetLupKey(DiffEvent.Modified, DiffEvent.Deleted), DiffType.LocationConflict},
                                                             {GetLupKey(DiffEvent.Modified, DiffEvent.Renamed), DiffType.LocationConflict},
                                                             {GetLupKey(DiffEvent.Modified, DiffEvent.Modified), DiffType.ContentConflict},

                                                             {GetLupKey(DiffEvent.Renamed,  DiffEvent.Unchanged), DiffType.Safe},
                                                             {GetLupKey(DiffEvent.Renamed,  DiffEvent.Deleted), DiffType.LocationConflict},
                                                             {GetLupKey(DiffEvent.Renamed,  DiffEvent.Renamed), DiffType.LocationConflict},
                                                             {GetLupKey(DiffEvent.Renamed,  DiffEvent.Modified), DiffType.LocationConflict},

                                                             {GetLupKey(DiffEvent.Deleted,  DiffEvent.Unchanged), DiffType.Safe},
                                                             {GetLupKey(DiffEvent.Deleted,  DiffEvent.Deleted), DiffType.Safe},
                                                             {GetLupKey(DiffEvent.Deleted,  DiffEvent.Renamed), DiffType.LocationConflict},
                                                             {GetLupKey(DiffEvent.Deleted,  DiffEvent.Modified), DiffType.LocationConflict},
                                                         };
        public Diff(T source, DiffEvent sourceEvent, T target, DiffEvent targetEvent, T meta)
        {
            Source = source;
            SourceEvent = sourceEvent;
            Target = target;
            TargetEvent = targetEvent;
            Meta = meta;
        }
    }
}
